/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package managedBeans;

import facade.SuiLibFacade;
import facade.exception.NotExistingObjectException;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;
import valueObject.CategoryVO;

/**
 *
 * @author David
 */
@ManagedBean
public class categoriesMB {
    @EJB
    SuiLibFacade suilibFacade;
    //Nombre de la categoria que se viene del inputText
    private String name;
    private boolean success;
    private boolean created = false;
    //Lista de categorias que se traen de la base de datos
    private List<CategoryVO> categories;
    /**
     * Creates a new instance of categoriesMB
     */
    public categoriesMB() {
    }
    
    public String getName() {
        return name;
    }

    public boolean getSuccess() {
        return success;
    }
    
    public boolean getCreated() {
        return created;
    }
    /**
     * @param name the name to set
     */
    public void setName(String name) {
        this.name = name;
    }
    
    public void createCategory(ActionEvent actionEvent){
        CategoryVO categoryVO = new CategoryVO();
        categoryVO.setName(getName());
        try {
           suilibFacade.createCategory(categoryVO);
           success = true;
           created = true;
        } catch (Exception ex) {
            success = false;
            created = true;
            Logger.getLogger(categoriesMB.class.getName()).log(Level.SEVERE, null, ex);
            
        }
    }

    /**
     * @return the categories
     */
    public List<CategoryVO> getCategories() {
        categories = suilibFacade.getAllCategoriesVO();
        return categories;
    }

    /**
     * @param categories the categories to set
     */
    public void setCategories(List<CategoryVO> categories) {
        //this.categories = categories;
    }
    
    public String outcome(){
		FacesContext fc = FacesContext.getCurrentInstance();
                Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
                this.setName(params.get("categoryName"));
		return "category";
   }
}